package com.sun.commonadapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


public class ViewHolder {
    private SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;

    public ViewHolder(Context context,ViewGroup parent,int layoutId,int position) {
        this.mPosition = position;
        this.mViews = new SparseArray<View>();

        mConvertView = LayoutInflater.from(context).inflate(layoutId,parent,false);
        mConvertView.setTag(this);

    }

    public  static  ViewHolder get(Context context,View convertView,ViewGroup parent,
                                   int layoutId,int position){
        if(convertView == null){
            return  new ViewHolder(context,parent,layoutId,position);
        }else {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            holder.mPosition = position;
            return  holder;
        }

    }

    /**
     * 通过viewId获取View
     * @param viewId
     * @param <T>
     * @return
     */
    public  <T extends View> T getView(int viewId){
        View view = mViews.get(viewId);
        if(view == null){
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }
    public View getmConvertView(){
        return  mConvertView;
    }

    /**
     * 设置TextView值
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId,String text){
        TextView tv = getView(viewId);
        tv.setText(text);
        return this;
    }

    public ViewHolder setImageResourcr(int viewId,int resId){
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    public ViewHolder setImageBitmap(int viewId,Bitmap bitmap){
        ImageView iv = getView(viewId);
        iv.setImageBitmap(bitmap);
        return this;
    }

    public ViewHolder setImageURL(int viewId,String url){
        ImageView iv = getView(viewId);
        //ImagetLoader.getIntance().loadImg(view,url)
        return this;
    }

}
